package com.leihub.xlfiletools.excel.utils;


import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author lei
 * @version 1.0
 * @description: TODO
 * @date 2024/7/22 21:48
 */

public class ExcelUtil {

    // 定义DataMapper接口
    public interface DataMapper<T> {
        Object[] map(T data);
    }

    // 创建样式
    private CellStyle createCellStyle(Workbook workbook, short color, boolean isBold) {
        Font font = workbook.createFont();
        font.setBold(isBold);
        font.setColor(IndexedColors.WHITE.getIndex());
        CellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(color);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        style.setFont(font);
        style.setWrapText(true);
        return style;
    }

    // 设置单元格值
    private void setCellValue(Cell cell, Object value, CellStyle wrapTextStyle, CellStyle rowStyle) {
        cell.setCellStyle(wrapTextStyle);
        if (value instanceof String) {
            cell.setCellValue((String) value);
        } else if (value instanceof Number) {
            cell.setCellValue(((Number) value).doubleValue());
        } else if (value instanceof Boolean) {
            cell.setCellValue((Boolean) value);
        } else if (value instanceof Date) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            cell.setCellValue(dateFormat.format((Date) value));
        } else {
            cell.setCellValue(value != null ? value.toString() : "");
        }
        cell.setCellStyle(rowStyle);
    }

    // 填充多个表格数据
    public  <T> void fillMultipleSheets(Sheet sheet, List<String> sheetNames, List<String[]> headersList, List<List<? extends T>> dataList, List<? extends DataMapper<? super T>> mappers, int columnWidth, int emptyRowCount) {
        Workbook workbook = sheet.getWorkbook();
        CellStyle headerStyle = createCellStyle(workbook, IndexedColors.DARK_BLUE.getIndex(), true);
        CellStyle evenRowStyle = createCellStyle(workbook, IndexedColors.LIGHT_GREEN.getIndex(), false);
        CellStyle oddRowStyle = createCellStyle(workbook, IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex(), false);
        CellStyle wrapTextStyle = workbook.createCellStyle();
        wrapTextStyle.setWrapText(true);

        int rowNum = 0;

        for (int i = 0; i < headersList.size(); i++) {
            String sheetName = sheetNames.get(i);
            Row sheetNameRow = sheet.createRow(rowNum++);
            Cell sheetNameCell = sheetNameRow.createCell(0);
            sheetNameCell.setCellValue(sheetName);
            sheetNameCell.setCellStyle(headerStyle);

            String[] headers = headersList.get(i);
            List<? extends T> data = dataList.get(i);
            DataMapper<? super T> mapper = mappers.get(i);

            // 创建表头
            Row headerRow = sheet.createRow(rowNum++);
            for (int colNum = 0; colNum < headers.length; colNum++) {
                Cell cell = headerRow.createCell(colNum);
                cell.setCellValue(headers[colNum]);
                cell.setCellStyle(headerStyle);
                sheet.setColumnWidth(colNum, columnWidth * 256); // 设置单元格宽度
            }

            // 填充数据
            for (T rowData : data) {
                Row row = sheet.createRow(rowNum++);
                CellStyle rowStyle = (rowNum % 2 == 0) ? evenRowStyle : oddRowStyle;
                Object[] values = mapper.map(rowData);
                for (int colNum = 0; colNum < values.length; colNum++) {
                    Cell cell = row.createCell(colNum);
                    setCellValue(cell, values[colNum], wrapTextStyle, rowStyle);
                }
            }

            // 在表格之间插入空白行（除最后一个表格外）
            if (i < headersList.size() - 1) {
                rowNum += emptyRowCount;
            }
        }
    }



}
